package com.clickpaas.materialsprocurement.service.Impl;

import com.bizcloud.ipaas.t0ba0ba6afb694026b6b4e4e048af2b43.d20210202135416.model.PurchaseOrderDTOResponse;
import com.clickpaas.materialsprocurement.dao.PurchaseOrderDao;
import com.clickpaas.materialsprocurement.entity.PurchaseOrderEntity;
import com.clickpaas.materialsprocurement.service.PurchaseOrderService;
import com.clickpaas.materialsprocurement.utils.DataInfoUtil;
import com.clickpaas.materialsprocurement.utils.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class PurchaseOrderServiceImpl implements PurchaseOrderService {
    @Resource
    private PurchaseOrderDao purchaseOrderDao;
    /**
     * 获取采购订单数据
     * @return
     */
    public List<PurchaseOrderDTOResponse> getPurchaseOrderInfo(PurchaseOrderEntity purchaseOrderEntity){

        List<PurchaseOrderDTOResponse> reUnitList;
        try {
            /**
             * 入参为空,不查询
             */
            if (null == purchaseOrderEntity || DataInfoUtil.isAllFieldNull(purchaseOrderEntity)){
                return null;
            }else {
                reUnitList = purchaseOrderDao.getPurchaseOrderByCondtion(purchaseOrderEntity);
            }
        } catch (Exception e) {
            log.info("入参对象异常,请检查：【{}】,报错信息：【{}】",purchaseOrderEntity,e);
            return null;
        }
        if (null != reUnitList && reUnitList.size()>0){
            return reUnitList;
        }
        return null;
    }

    /**
     * 采购订单数据转换为bean
     * @return
     */
    public List<PurchaseOrderEntity> purchaseOrderDataConver(List<PurchaseOrderDTOResponse> list){
        List<PurchaseOrderEntity> Entities = new ArrayList<>();
        /**
         * 多线程处理数据
         */
        ExecutorService pool = Executors.newCachedThreadPool();
        CountDownLatch latch = new CountDownLatch(list.size());
        for (PurchaseOrderDTOResponse data : list){
            /**
             * 开启子线程
             */
            Runnable run = new Runnable() {
                public void run() {
                    try {
                        PurchaseOrderEntity unitEntity = new PurchaseOrderEntity();
                        SpringUtil.copyPropertiesIgnoreNull(data,unitEntity);
                        Entities.add(unitEntity);
                    }catch(Exception e){
                        log.info("多线程转换报错,请检查：【{}】",e);
                    } finally {
                        latch.countDown();
                    }
                }
            };
            pool.execute(run);
        }try {
            latch.await(10, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            log.info("多线程转换报错,请检查：【{}】",e);
        }finally {
            pool.shutdown();
        }
        if (null != Entities && Entities.size()>0){
            return Entities;
        }
        return null;
    }
}
